#include "ftpserver.h"
#include "ftpsession.h"

FtpServer::FtpServer(QObject *parent) : QTcpServer(parent)
{
    connect(this, &FtpServer::acceptError, this, &FtpServer::onAcceptError);
}

FtpServer::~FtpServer()
{
    stop();
}

void FtpServer::setConfig(const ServerConfig &config)
{
    m_serverConfig = config;
}

bool FtpServer::start()
{
    return listen(QHostAddress::AnyIPv4, 21);
}

void FtpServer::stop()
{
    close();
}

void FtpServer::onAcceptError(QAbstractSocket::SocketError error)
{
    qDebug() << "accept error" << error << errorString();
}

void FtpServer::incomingConnection(qintptr handle)
{
    auto ftpSession = new FtpSession(handle, m_serverConfig, this);
    ftpSession->start();
}
